package com.ecy.quadratic.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;

import com.ecy.quadratic.R;
import com.ecy.quadratic.reader.base.BaseActivity;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.ui.fragment.ComicBrowserFragment;
import com.ecy.quadratic.ui.fragment.NovelBrowserFragment;
import com.ecy.quadratic.widget.PagerSlidingTabStrip;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

public class BrowserActivity extends BaseActivity {

    @Bind(R.id.tabs)
    PagerSlidingTabStrip tabLayout;
    @Bind(R.id.tab_viewpager)
    ViewPager mViewPager;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, BrowserActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_browser;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    public void initToolBar() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void configViews() {
        final ArrayList<String> titles = new ArrayList<String>();
        titles.add("漫画");
        titles.add("小说");
        final Fragment[] fragments = new Fragment[]{new ComicBrowserFragment(), new NovelBrowserFragment()};
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {  //注释掉，因为tablayout导航卡名字是根据这里进行绑定的，现在我们自定义了，不需要了
                return titles.get(position);
            }

        });
        tabLayout.setOnTabReselectedListener(new PagerSlidingTabStrip.OnTabReselectedListener() {
            @Override
            public void onTabReselected(int position) {
                mViewPager.setCurrentItem(position);
            }
        });
        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                .getDisplayMetrics());
        mViewPager.setPageMargin(pageMargin);
        mViewPager.setCurrentItem(0);
        tabLayout.setViewPager(mViewPager);
    }

    @OnClick(R.id.title_back_iv)
    public void back() {
        finish();
    }
}
